import jieba
from myapp.utils.connection import POOL


# 词云的处理函数, 输入关键词，返回对应格式的json
# 里面数据库的参数可能要修改
def word_cloud_json_data(keyword):
    conn = POOL.connection()
    # conn = pymysql.connect(host="localhost", user='root', password='20000908q',database="database", charset='utf8')
    cursor = conn.cursor()
    sql = 'SELECT text from {}_advsearch'.format(keyword)
    cursor.execute(sql)
    result = {}
    for sub_text in cursor.fetchall():
        sub_text = jieba.lcut(sub_text[0])
        for word in sub_text:
            if word in result.keys():
                result[word] = result[word] + 1
            else:
                result[word] = 1

    raw_keys = list(result.keys())
    for key in raw_keys:
        if '#' in key or key.__len__() == 1 or result[key] < 10:
            result.pop(key)

    final_result = {}
    ky_list = []
    # 排序，取最大100项
    sorted_list = sorted(
        result.items(), key=lambda item: item[1], reverse=True)
    print(len(sorted_list))
    size = min(len(sorted_list), 200)
    for index in range(0, size):
        item = sorted_list[index]
        temp_dic = {}
        temp_dic['name'] = item[0]
        temp_dic['value'] = item[1]
        ky_list.append(temp_dic)
    final_result['data'] = ky_list

    # conn.close()
    cursor.close()
    # print(final_result)
    return final_result


# 地图的处理函数
def map_cloud_json_data(keyword):
    conn = POOL.connection()
    # conn = pymysql.connect(host="localhost", user='root', password='20000908q', database="database", charset='utf8')
    cursor = conn.cursor()

    sql = 'SELECT location from {}_userspider'.format(keyword)
    cursor.execute(sql)

    result = {}

    for sub_text in cursor.fetchall():
        location = sub_text[0].split()[0]
        if '海外' in location or '其它' in location:
            continue
        if location in result.keys():
            result[location] = result[location] + 1
        else:
            result[location] = 1

    final_result = {}
    ky_list = []
    for key in result:
        temp_dic = {}
        temp_dic['name'] = key
        temp_dic['value'] = result[key]
        ky_list.append(temp_dic)
    final_result['data'] = ky_list

    # conn.close()
    cursor.close()
    # print(final_result)
    return final_result
